package com.knife.bbs.action;

import javax.servlet.http.HttpServletResponse;
import com.knife.web.ActionContext;
import com.knife.web.IWebAction;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import java.awt.image.*;
import javax.imageio.*;
import com.knife.util.*;
import com.knife.web.tools.ImageCode;

public class RandomImgCode implements IWebAction {
	public Page execute(WebForm form, Module module) throws Exception {
		int number = 0;
		number = CommUtil.null2Int(form.get("number"));
		int max = 0;
		max = CommUtil.null2Int(form.get("max"));
		System.out.println(max);
		HttpServletResponse response = ActionContext.getContext().getResponse();
		response.setContentType("image/jpeg");
		ImageCode image = new ImageCode();
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			// 输出图象到页面
			BufferedImage img = null;
			if (number != 0) {
				if (max != 0) {
					img = image.creatImage3D(number, max);
				} else {
					img = image.creatImage(number);
				}
			} else {
				img = image.creatImage();
				ActionContext.getContext().getSession().setAttribute("rand",
						image.getSRand());

			}
			ImageIO.write(img, "JPEG", response.getOutputStream());
			response.getOutputStream().flush();
			response.getOutputStream().close();
		} catch (Exception e) {
			System.out.println("错误:" + e);
		}
		return null;
	}

}
